package vn.conghuy.talkwithme.responsor.responsor;

import vn.conghuy.talkwithme.data.livescore.LivescoreParser;
import vn.conghuy.talkwithme.data.livescore.LivescoreWinLoseTip;
import vn.conghuy.talkwithme.data.livescore.LivescoreWinLoseTipMgr;
import vn.conghuy.talkwithme.data.sqlmodel.SqlData;
import vn.conghuy.talkwithme.utils.StringWrapper;
import vn.conghuy.talkwithme.utils.Utils;
import android.content.Context;

public class FootballResponsor extends Responsor {
	
	private static final String OF_KEYWORD = "of";
	
	LivescoreParser livescoreParser;
	LivescoreWinLoseTipMgr winLoseTipMgr;
	public FootballResponsor() {
		livescoreParser = new LivescoreParser();
		winLoseTipMgr = new LivescoreWinLoseTipMgr();
	}

	public FootballResponsor(Context context) {
		super(context);
		livescoreParser = new LivescoreParser();
		winLoseTipMgr = new LivescoreWinLoseTipMgr();
	}
	
	@Override
	public String response(String request) {
		
		if(Utils.isExistStringInArray(SqlData.stopWords, request) == true){
			relativeLastResponse = RELATIVE_NONE;
			return Utils.getRandomString(SqlData.callCancelPhrases);
		}
		
		
		StringWrapper keyWordWrapper = new StringWrapper();
		StringWrapper mainKeywordWrapper = new StringWrapper();
		StringWrapper subKeywordWrapper = new StringWrapper();
		
		if(Utils.isExistElemInArrayIsWordOfStr(SqlData.livescoreKeywords, request, keyWordWrapper)){
			int indexOfKeyword = request.indexOf(keyWordWrapper.getStr());
			String keyWord = keyWordWrapper.getStr();
			indexOfKeyword += keyWord.length() +1;
			if(indexOfKeyword < request.length()){
				String leaguePhrase = request.substring(indexOfKeyword);
				if(leaguePhrase.startsWith(OF_KEYWORD) && leaguePhrase.length() > OF_KEYWORD.length() + 1){
					leaguePhrase = leaguePhrase.substring(OF_KEYWORD.length() + 1);
				}
				else leaguePhrase = "";
				
				if(leaguePhrase.length() > 0){
					relativeLastResponse = RELATIVE_NONE;
					livescoreParser.update();
					return livescoreParser.leagueLiveScore(leaguePhrase);
				}
			}
				
		}
		
		
		else if(Utils.isExistElemInArrayIsWordOfStr(SqlData.livescoreTipMainKeywords, request, mainKeywordWrapper)
				&&Utils.isExistElemInArrayIsWordOfStr(SqlData.livescoreTipSubKeywords, request, subKeywordWrapper)){
			int mainKeywordIndex = request.indexOf(mainKeywordWrapper.getStr());
			int subKeywordIndex = request.indexOf(subKeywordWrapper.getStr());
			if(Math.abs(mainKeywordIndex - subKeywordIndex) < 15){
				winLoseTipMgr.update();
				relativeLastResponse = RELATIVE_NONE;
				return winLoseTipMgr.toHtmlString();
			}
		}
		relativeLastResponse = RELATIVE_NONE;
		return super.response(request);
		
	}
	
	@Override
	public int indexOfMainVerb(String request) {
		StringWrapper keyWordWrapper = new StringWrapper();
		if(Utils.isExistElemInArrayIsWordOfStr(SqlData.livescoreKeywords, request, keyWordWrapper)){
			return Utils.indexOfWord(request, keyWordWrapper.getStr());
		}
		return super.indexOfMainVerb(request);
	}
}
